﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Threading;
using LaunchMeNot.Core;
using LaunchMeNot.DAL;
using NDesk.Options;

namespace LaunchMeNot
{
    /// <summary>
    /// 
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
            FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(System.Windows.Markup.XmlLanguage.GetLanguage(System.Globalization.CultureInfo.CurrentCulture.IetfLanguageTag)));
        }

        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            WinUnhandledException dlg = new WinUnhandledException(e.Exception);
            dlg.ShowDialog();
            e.Handled = true;
        }

        /// <summary>
        /// Parses the startup arguments then starts the application.
        /// </summary>
        /// <param name="e">A <see cref="T:System.Windows.StartupEventArgs"/> that contains the event data.</param>
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            WPFConfigurationManager.ConfigureDefault();
            WinMain mainWindow;
            if (e.Args != null && e.Args.Length > 0) {
                bool launchOnly = false,
                     showHelp = false;
                bool? canAbort = null;
                string configFile = null;
                OptionSet opts = new OptionSet() {
                    { "l|launch:", "Start AutoLaunch", v=> launchOnly = true },
                    { "c|config:", "Specify an alternate configuration file", v=> configFile = v },
                    { "preventabort:", "When used with the -l or -launch parameter, prevents the AutoLaunch process from being aborted or cancelled.",delegate(bool v) { canAbort = false; }
                    },
                    { "h|?|help", "Show help", v=> showHelp = true },
                };
                opts.Parse(e.Args);
                if (showHelp == true) {
                    Console.WriteLine("Check the help file or http://wiki.lime49.com for more information");
                    opts.WriteOptionDescriptions(Console.Out);
                } else {
                    if (configFile != null) {
                        //WPFConfigurationManager.FilePath = configFile;
                        DALConfigurationManager.LaunchMeNotStore.FilePath = configFile;
                    } else {
                        WPFConfigurationManager.EnsureDatabaseValid();
                    }
                    if (canAbort != null) {
                        CoreConfigurationManager.AllowCancelAutoLaunch = canAbort.Value;
                    }
                    if (launchOnly) {
                        WinLaunch launcher = new WinLaunch();
                        launcher.ShowDialog();
                    } else {
                        mainWindow = new WinMain();
                        mainWindow.ShowDialog();
                    }
                }
            } else {
                WPFConfigurationManager.EnsureDatabaseValid();
                mainWindow = new WinMain();
                mainWindow.ShowDialog();
            }
            Shutdown();
        }
    }
}
